package edu.dlust.trading_platform.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import edu.dlust.trading_platform.DataBean.GoodsBean;
import edu.dlust.trading_platform.R;

public class BuyAdapter extends BaseAdapter {
    private List<GoodsBean> list;
    private LayoutInflater layoutInflater;

    public BuyAdapter(Context context, List<GoodsBean> list) {
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list==null ? 0: list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View converView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(converView == null){
            converView = layoutInflater.inflate(R.layout.item_buy_layout,null);
            viewHolder = new ViewHolder(converView);
            converView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) converView.getTag();
        }
        GoodsBean goodsInfo = (GoodsBean) getItem(position);
            viewHolder.id.setText(String.valueOf(goodsInfo.getGid()));
            viewHolder.content.setText("[已购买]"+goodsInfo.getGContent());
            viewHolder.name.setText("发布者： "+goodsInfo.getGName());
            viewHolder.price.setText("购买者： "+goodsInfo.getGBuyer_name());//
            viewHolder.condition.setText("卖家联系电话： "+goodsInfo.getGBuyer_Phone());//
            viewHolder.detail.setText("请尽快联系卖家");//
            viewHolder.image.setBackgroundResource(R.drawable.noimage);
            try{
                viewHolder.image.setImageBitmap(goodsInfo.getGImage());
            } catch (Exception e) {
            }

        return converView;


    }
    class ViewHolder{
        TextView id,content,name,price,condition,detail;
        ImageView image;
        public ViewHolder(View view){
            id = (TextView) view.findViewById(R.id.goods_id);
            content = (TextView) view.findViewById(R.id.tv_goods_name);
            name = (TextView) view.findViewById(R.id.tv_goods_publisher);
            price = (TextView) view.findViewById(R.id.tv_goods_price);
            condition = (TextView) view.findViewById(R.id.tv_goods_condition);
            detail = (TextView) view.findViewById(R.id.tv_goods_detail);
            image = (ImageView) view.findViewById(R.id.iv_goods_image);
        }
    }
}
